﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using BesAsm.MasterData.Entities;
using BesAsm.MasterData.Data;

#endregion

namespace BesAsm.MasterData.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ZoneProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ZoneProviderBaseCore : EntityProviderBase<BesAsm.MasterData.Entities.Zone, BesAsm.MasterData.Entities.ZoneKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, BesAsm.MasterData.Entities.ZoneKey key)
		{
			return Delete(transactionManager, key.ZoneId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_zoneId">Primary key. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.String _zoneId)
		{
			return Delete(null, _zoneId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_zoneId">Primary key. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.String _zoneId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override BesAsm.MasterData.Entities.Zone Get(TransactionManager transactionManager, BesAsm.MasterData.Entities.ZoneKey key, int start, int pageLength)
		{
			return GetByZoneId(transactionManager, key.ZoneId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ZONES index.
		/// </summary>
		/// <param name="_zoneId">Primary key</param>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByZoneId(System.String _zoneId)
		{
			int count = -1;
			return GetByZoneId(null,_zoneId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ZONES index.
		/// </summary>
		/// <param name="_zoneId">Primary key</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByZoneId(System.String _zoneId, int start, int pageLength)
		{
			int count = -1;
			return GetByZoneId(null, _zoneId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ZONES index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_zoneId">Primary key</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByZoneId(TransactionManager transactionManager, System.String _zoneId)
		{
			int count = -1;
			return GetByZoneId(transactionManager, _zoneId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ZONES index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_zoneId">Primary key</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByZoneId(TransactionManager transactionManager, System.String _zoneId, int start, int pageLength)
		{
			int count = -1;
			return GetByZoneId(transactionManager, _zoneId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ZONES index.
		/// </summary>
		/// <param name="_zoneId">Primary key</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByZoneId(System.String _zoneId, int start, int pageLength, out int count)
		{
			return GetByZoneId(null, _zoneId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ZONES index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_zoneId">Primary key</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public abstract BesAsm.MasterData.Entities.Zone GetByZoneId(TransactionManager transactionManager, System.String _zoneId, int start, int pageLength, out int count);
						
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key UQ_ZONES_name index.
		/// </summary>
		/// <param name="_name">Name of general zoning code</param>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByName(System.String _name)
		{
			int count = -1;
			return GetByName(null,_name, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the UQ_ZONES_name index.
		/// </summary>
		/// <param name="_name">Name of general zoning code</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByName(System.String _name, int start, int pageLength)
		{
			int count = -1;
			return GetByName(null, _name, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the UQ_ZONES_name index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_name">Name of general zoning code</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByName(TransactionManager transactionManager, System.String _name)
		{
			int count = -1;
			return GetByName(transactionManager, _name, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the UQ_ZONES_name index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_name">Name of general zoning code</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByName(TransactionManager transactionManager, System.String _name, int start, int pageLength)
		{
			int count = -1;
			return GetByName(transactionManager, _name, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the UQ_ZONES_name index.
		/// </summary>
		/// <param name="_name">Name of general zoning code</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public BesAsm.MasterData.Entities.Zone GetByName(System.String _name, int start, int pageLength, out int count)
		{
			return GetByName(null, _name, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the UQ_ZONES_name index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_name">Name of general zoning code</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="BesAsm.MasterData.Entities.Zone"/> class.</returns>
		public abstract BesAsm.MasterData.Entities.Zone GetByName(TransactionManager transactionManager, System.String _name, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Zone&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Zone&gt;"/></returns>
		public static TList<Zone> Fill(IDataReader reader, TList<Zone> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				BesAsm.MasterData.Entities.Zone c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Zone")
					.Append("|").Append((System.String)reader[((int)ZoneColumn.ZoneId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Zone>(
					key.ToString(), // EntityTrackingKey
					"Zone",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new BesAsm.MasterData.Entities.Zone();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.ZoneId = (System.String)reader[((int)ZoneColumn.ZoneId - 1)];
					c.OriginalZoneId = c.ZoneId;
					c.Name = (System.String)reader[((int)ZoneColumn.Name - 1)];
					c.Description = (reader.IsDBNull(((int)ZoneColumn.Description - 1)))?null:(System.String)reader[((int)ZoneColumn.Description - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="BesAsm.MasterData.Entities.Zone"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="BesAsm.MasterData.Entities.Zone"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, BesAsm.MasterData.Entities.Zone entity)
		{
			if (!reader.Read()) return;
			
			entity.ZoneId = (System.String)reader[((int)ZoneColumn.ZoneId - 1)];
			entity.OriginalZoneId = (System.String)reader["zone_id"];
			entity.Name = (System.String)reader[((int)ZoneColumn.Name - 1)];
			entity.Description = (reader.IsDBNull(((int)ZoneColumn.Description - 1)))?null:(System.String)reader[((int)ZoneColumn.Description - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="BesAsm.MasterData.Entities.Zone"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="BesAsm.MasterData.Entities.Zone"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, BesAsm.MasterData.Entities.Zone entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ZoneId = (System.String)dataRow["zone_id"];
			entity.OriginalZoneId = (System.String)dataRow["zone_id"];
			entity.Name = (System.String)dataRow["name"];
			entity.Description = Convert.IsDBNull(dataRow["description"]) ? null : (System.String)dataRow["description"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="BesAsm.MasterData.Entities.Zone"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">BesAsm.MasterData.Entities.Zone Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, BesAsm.MasterData.Entities.Zone entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByZoneId methods when available
			
			#region DscCollectionGetByGeneralZoneEx
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<Dsc>|DscCollectionGetByGeneralZoneEx", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DscCollectionGetByGeneralZoneEx' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.DscCollectionGetByGeneralZoneEx = DataRepository.DscProvider.GetByGeneralZoneEx(transactionManager, entity.ZoneId);

				if (deep && entity.DscCollectionGetByGeneralZoneEx.Count > 0)
				{
					deepHandles.Add("DscCollectionGetByGeneralZoneEx",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<Dsc>) DataRepository.DscProvider.DeepLoad,
						new object[] { transactionManager, entity.DscCollectionGetByGeneralZoneEx, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region DetailedZoneCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<DetailedZone>|DetailedZoneCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DetailedZoneCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.DetailedZoneCollection = DataRepository.DetailedZoneProvider.GetByZoneId(transactionManager, entity.ZoneId);

				if (deep && entity.DetailedZoneCollection.Count > 0)
				{
					deepHandles.Add("DetailedZoneCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<DetailedZone>) DataRepository.DetailedZoneProvider.DeepLoad,
						new object[] { transactionManager, entity.DetailedZoneCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region AsmZoningCollectionGetByGeneralZoneCp
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<AsmZoning>|AsmZoningCollectionGetByGeneralZoneCp", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'AsmZoningCollectionGetByGeneralZoneCp' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.AsmZoningCollectionGetByGeneralZoneCp = DataRepository.AsmZoningProvider.GetByGeneralZoneCp(transactionManager, entity.ZoneId);

				if (deep && entity.AsmZoningCollectionGetByGeneralZoneCp.Count > 0)
				{
					deepHandles.Add("AsmZoningCollectionGetByGeneralZoneCp",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<AsmZoning>) DataRepository.AsmZoningProvider.DeepLoad,
						new object[] { transactionManager, entity.AsmZoningCollectionGetByGeneralZoneCp, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region DscCollectionGetByGeneralZoneCp
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<Dsc>|DscCollectionGetByGeneralZoneCp", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DscCollectionGetByGeneralZoneCp' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.DscCollectionGetByGeneralZoneCp = DataRepository.DscProvider.GetByGeneralZoneCp(transactionManager, entity.ZoneId);

				if (deep && entity.DscCollectionGetByGeneralZoneCp.Count > 0)
				{
					deepHandles.Add("DscCollectionGetByGeneralZoneCp",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<Dsc>) DataRepository.DscProvider.DeepLoad,
						new object[] { transactionManager, entity.DscCollectionGetByGeneralZoneCp, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region AsmZoningCollectionGetByGeneralZoneEx
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<AsmZoning>|AsmZoningCollectionGetByGeneralZoneEx", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'AsmZoningCollectionGetByGeneralZoneEx' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.AsmZoningCollectionGetByGeneralZoneEx = DataRepository.AsmZoningProvider.GetByGeneralZoneEx(transactionManager, entity.ZoneId);

				if (deep && entity.AsmZoningCollectionGetByGeneralZoneEx.Count > 0)
				{
					deepHandles.Add("AsmZoningCollectionGetByGeneralZoneEx",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<AsmZoning>) DataRepository.AsmZoningProvider.DeepLoad,
						new object[] { transactionManager, entity.AsmZoningCollectionGetByGeneralZoneEx, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the BesAsm.MasterData.Entities.Zone object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">BesAsm.MasterData.Entities.Zone instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">BesAsm.MasterData.Entities.Zone Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, BesAsm.MasterData.Entities.Zone entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
	
			#region List<Dsc>
				if (CanDeepSave(entity.DscCollectionGetByGeneralZoneEx, "List<Dsc>|DscCollectionGetByGeneralZoneEx", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(Dsc child in entity.DscCollectionGetByGeneralZoneEx)
					{
						if(child.GeneralZoneExSource != null)
						{
							child.GeneralZoneEx = child.GeneralZoneExSource.ZoneId;
						}
						else
						{
							child.GeneralZoneEx = entity.ZoneId;
						}

					}

					if (entity.DscCollectionGetByGeneralZoneEx.Count > 0 || entity.DscCollectionGetByGeneralZoneEx.DeletedItems.Count > 0)
					{
						//DataRepository.DscProvider.Save(transactionManager, entity.DscCollectionGetByGeneralZoneEx);
						
						deepHandles.Add("DscCollectionGetByGeneralZoneEx",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< Dsc >) DataRepository.DscProvider.DeepSave,
							new object[] { transactionManager, entity.DscCollectionGetByGeneralZoneEx, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<DetailedZone>
				if (CanDeepSave(entity.DetailedZoneCollection, "List<DetailedZone>|DetailedZoneCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(DetailedZone child in entity.DetailedZoneCollection)
					{
						if(child.ZoneIdSource != null)
						{
							child.ZoneId = child.ZoneIdSource.ZoneId;
						}
						else
						{
							child.ZoneId = entity.ZoneId;
						}

					}

					if (entity.DetailedZoneCollection.Count > 0 || entity.DetailedZoneCollection.DeletedItems.Count > 0)
					{
						//DataRepository.DetailedZoneProvider.Save(transactionManager, entity.DetailedZoneCollection);
						
						deepHandles.Add("DetailedZoneCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< DetailedZone >) DataRepository.DetailedZoneProvider.DeepSave,
							new object[] { transactionManager, entity.DetailedZoneCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<AsmZoning>
				if (CanDeepSave(entity.AsmZoningCollectionGetByGeneralZoneCp, "List<AsmZoning>|AsmZoningCollectionGetByGeneralZoneCp", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(AsmZoning child in entity.AsmZoningCollectionGetByGeneralZoneCp)
					{
						if(child.GeneralZoneCpSource != null)
						{
							child.GeneralZoneCp = child.GeneralZoneCpSource.ZoneId;
						}
						else
						{
							child.GeneralZoneCp = entity.ZoneId;
						}

					}

					if (entity.AsmZoningCollectionGetByGeneralZoneCp.Count > 0 || entity.AsmZoningCollectionGetByGeneralZoneCp.DeletedItems.Count > 0)
					{
						//DataRepository.AsmZoningProvider.Save(transactionManager, entity.AsmZoningCollectionGetByGeneralZoneCp);
						
						deepHandles.Add("AsmZoningCollectionGetByGeneralZoneCp",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< AsmZoning >) DataRepository.AsmZoningProvider.DeepSave,
							new object[] { transactionManager, entity.AsmZoningCollectionGetByGeneralZoneCp, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<Dsc>
				if (CanDeepSave(entity.DscCollectionGetByGeneralZoneCp, "List<Dsc>|DscCollectionGetByGeneralZoneCp", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(Dsc child in entity.DscCollectionGetByGeneralZoneCp)
					{
						if(child.GeneralZoneCpSource != null)
						{
							child.GeneralZoneCp = child.GeneralZoneCpSource.ZoneId;
						}
						else
						{
							child.GeneralZoneCp = entity.ZoneId;
						}

					}

					if (entity.DscCollectionGetByGeneralZoneCp.Count > 0 || entity.DscCollectionGetByGeneralZoneCp.DeletedItems.Count > 0)
					{
						//DataRepository.DscProvider.Save(transactionManager, entity.DscCollectionGetByGeneralZoneCp);
						
						deepHandles.Add("DscCollectionGetByGeneralZoneCp",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< Dsc >) DataRepository.DscProvider.DeepSave,
							new object[] { transactionManager, entity.DscCollectionGetByGeneralZoneCp, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<AsmZoning>
				if (CanDeepSave(entity.AsmZoningCollectionGetByGeneralZoneEx, "List<AsmZoning>|AsmZoningCollectionGetByGeneralZoneEx", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(AsmZoning child in entity.AsmZoningCollectionGetByGeneralZoneEx)
					{
						if(child.GeneralZoneExSource != null)
						{
							child.GeneralZoneEx = child.GeneralZoneExSource.ZoneId;
						}
						else
						{
							child.GeneralZoneEx = entity.ZoneId;
						}

					}

					if (entity.AsmZoningCollectionGetByGeneralZoneEx.Count > 0 || entity.AsmZoningCollectionGetByGeneralZoneEx.DeletedItems.Count > 0)
					{
						//DataRepository.AsmZoningProvider.Save(transactionManager, entity.AsmZoningCollectionGetByGeneralZoneEx);
						
						deepHandles.Add("AsmZoningCollectionGetByGeneralZoneEx",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< AsmZoning >) DataRepository.AsmZoningProvider.DeepSave,
							new object[] { transactionManager, entity.AsmZoningCollectionGetByGeneralZoneEx, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ZoneChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>BesAsm.MasterData.Entities.Zone</c>
	///</summary>
	public enum ZoneChildEntityTypes
	{

		///<summary>
		/// Collection of <c>Zone</c> as OneToMany for DscCollection
		///</summary>
		[ChildEntityType(typeof(TList<Dsc>))]
		DscCollectionGetByGeneralZoneEx,

		///<summary>
		/// Collection of <c>Zone</c> as OneToMany for DetailedZoneCollection
		///</summary>
		[ChildEntityType(typeof(TList<DetailedZone>))]
		DetailedZoneCollection,

		///<summary>
		/// Collection of <c>Zone</c> as OneToMany for AsmZoningCollection
		///</summary>
		[ChildEntityType(typeof(TList<AsmZoning>))]
		AsmZoningCollectionGetByGeneralZoneCp,

		///<summary>
		/// Collection of <c>Zone</c> as OneToMany for DscCollection
		///</summary>
		[ChildEntityType(typeof(TList<Dsc>))]
		DscCollectionGetByGeneralZoneCp,

		///<summary>
		/// Collection of <c>Zone</c> as OneToMany for AsmZoningCollection
		///</summary>
		[ChildEntityType(typeof(TList<AsmZoning>))]
		AsmZoningCollectionGetByGeneralZoneEx,
	}
	
	#endregion ZoneChildEntityTypes
	
	#region ZoneFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ZoneColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Zone"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ZoneFilterBuilder : SqlFilterBuilder<ZoneColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ZoneFilterBuilder class.
		/// </summary>
		public ZoneFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ZoneFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ZoneFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ZoneFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ZoneFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ZoneFilterBuilder
	
	#region ZoneParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ZoneColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Zone"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ZoneParameterBuilder : ParameterizedSqlFilterBuilder<ZoneColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ZoneParameterBuilder class.
		/// </summary>
		public ZoneParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ZoneParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ZoneParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ZoneParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ZoneParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ZoneParameterBuilder
	
	#region ZoneSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ZoneColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Zone"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ZoneSortBuilder : SqlSortBuilder<ZoneColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ZoneSqlSortBuilder class.
		/// </summary>
		public ZoneSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ZoneSortBuilder
	
} // end namespace
